package com.hrzyit.whistle.user.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import com.alibaba.android.arouter.facade.annotation.Route
import com.hrzyit.base.ext.enable
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.user.R
import com.hrzyit.whistle.user.injection.component.DaggerUserComponent
import com.hrzyit.whistle.user.injection.module.UserModule
import com.hrzyit.whistle.user.presenter.ForgetPwdPresenter
import com.hrzyit.whistle.user.presenter.view.ForgetPwdView
import kotlinx.android.synthetic.main.activity_forget_pwd.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 忘记密码功能 Activity
 * @author YangWei
 * @date 2019/3/10
 */
@Route(path = RouterPath.UserCenter.PATH_FORGET_PWD)
class ForgetPwdActivity : BaseMvpActivity<ForgetPwdPresenter>(),
    ForgetPwdView, View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forget_pwd)

        initView()
    }

    /**
     * 初始化视图
     */
    private fun initView() {

        // 必填项输入完成后登录按钮可点击
        with(mNextBtn) {
            enable(mMobileEt) { isNextBtnEnable() }
            enable(mVerifyCodeEt) { isNextBtnEnable() }
        }

        mVerifyCodeBtn.onClick(this)

        mNextBtn.onClick(this)
    }

    /**
     * 判断按钮是否可用
     */
    private fun isNextBtnEnable(): Boolean {
        return mMobileEt.text.isNullOrEmpty().not() &&
                mVerifyCodeEt.text.isNullOrEmpty().not()
    }

    /**
     * 按钮点击事件
     */
    override fun onClick(view: View) {

        when (view.id) {
            R.id.mVerifyCodeBtn -> {
                // 获取短信验证码
                if (mMobileEt.text.isNotBlank()) {
                    mVerifyCodeBtn.requestSendVerifyNumber()
                    toast("发送验证成功")

                } else {
                    toast("请输入手机号")
                }
            }
            R.id.mNextBtn -> {
                // 验证验证码
                mPresenter.forgetPwd(mMobileEt.text.toString(), mVerifyCodeEt.text.toString())
            }
        }
    }

    /**
     * Dagger 注册
     */
    override fun injectComponent() {

        DaggerUserComponent.builder()
            .activityComponent(mActivityComponent)
            .userModule(UserModule())
            .build().inject(this)

        mPresenter.mView = this
    }

    /**
     * 成功回调：忘记密码
     */
    override fun onForgetPwdResult(result: String) {

        toast(result)

        // 跳转到重置密码界面
        startActivity<ResetPwdActivity>("mobile" to mMobileEt.text.toString())

    }
}